<?php
// +----------------------------------------------------------------------
// | najing [ 通用后台管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: lin
// +----------------------------------------------------------------------

namespace  app\admin\controller;

use app\common\controller\AdminController;
use app\common\model\GoodsComment;
use app\common\model\User as UserModel;
use app\common\model\UserGrade;
use app\common\model\Balance;
use app\common\model\UserLog;
use app\common\model\UserPointLog;
use http\Exception;
use think\facade\Request;
use think\facade\View;

/**
 * 会员管理
 * Class User
 * @package app\admin\controller
 */
class User extends  AdminController {

    /**
     * 用户列表
     * @author lin
     */
    public function  index() {

        if($this->request->isAjax())
        {
            $userModel = new UserModel();
            return $userModel->tableData(input('param.'));
        }

        return View::fetch();
    }

    /**
     * 添加用户
     * @author lin
     */
    public function  addUser() {


        if($this->request->isPost())
        {
            $input = Request::param();
            $userModel = new UserModel();
            $result = $userModel->manageAdd($input);
            return $result;
        }

        View::layout(false);
        View::assign('grade', []);
        $result['status'] = 0;
        $result['msg'] = '获取成功';
        $result['datas'] =   View::fetch();
        return $result;

    }

    /**
     * 编辑用户
     * @author lin
     */
    public function editUser()
    {
        $result = [
            'status' => false,
            'msg' => '失败',
            'datas' => ''
        ];

        $userModel = new UserModel();

        if($this->request->isPost())
        {
            $input = Request::param();
            $result = $userModel->manageEdit($input);
            return $result;
        }


        View::layout(false);
        View::assign('grade', []);
        $user_id = Request::param('user_id');
        $info = $userModel->getUserInfo($user_id);
        if(!$info['status']){
            return error_code(10000);
        }

        View::assign('info', $info['data']);
        $gradeModel = new UserGrade();
        $userGrade = $gradeModel->getAll();

        View::assign('grade', $userGrade);
        $result['status'] = 0;
        $result['msg'] = '获取成功';
        $result['datas'] = View::fetch();
        return $result;

    }


    /**
     * 编辑余额
     * @author lin
     */
    public function editMoney()
    {
        $result = [
            'status' => false,
            'msg' => '失败',
            'data' => ''
        ];

        View::layout(false);
        $user_id = input('user_id');
        $flag = input('flag', 'false');

        if($flag == 'true')
        {
            $money = input('money');
            $balanceMoney = new Balance();
            return $balanceMoney->change($user_id, $balanceMoney::TYPE_ADMIN, $money, 0);
        }

        View::assign('user_id', $user_id);
        $User = new UserModel();
        $where[] = ['id', '=', $user_id];
        $user_info = $User->where($where)->find();
        View::assign('money', $user_info['balance']);
        $result['status'] = 0;
        $result['msg'] = '';
        $result['datas'] = View::fetch();
        return $result;
    }

    /**
     * 删除用户
     * @author lin
     * @return array
     */
    public function delUser()
    {
        $result = [
            'status' => false,
            'msg' => '失败',
            'data' => ''
        ];

        $ids = input('ids/a', []);
        if(!$ids)
        {
            return $result;
        }
        $userModel = new UserModel();
        $res = $userModel::destroy($ids);
        if($res !== false)
        {
            $result['msg'] = '删除成功';
            $result['status'] = true;
        }
        $c = [];
        foreach($c as $item){
            $le[$item['id']] = $item['name'];
        }


        return $result;
    }

    /**
     * 用户等级列表
     * @author lin
     */
    public function grade()
    {
        if($this->request->isAjax())
        {
            $userGradeModel = new UserGrade();
            return $userGradeModel->tableData(input('param.'));
        }
        return View::fetch();
    }


    /**
     * 用户等级新增和编辑，都走这里
     * @author lin
     */
    public function gradeAdd()
    {
        $result = [
            'status' => false,
            'msg' => '失败',
            'data' => ''
        ];

        View::layout(false);

        $userGradeModel = new UserGrade();
        if($this->request->isPost())
        {
            $validate = new \app\common\validate\UserGrade();
            if (!$validate->check(input('param.')))
            {
                $result['msg'] = $validate->getError();
                return $result;
            }
            return $userGradeModel->toEdit(input('param.id'), input('param.name'), input('param.is_def', 2));
        }

        if(input('?param.id'))
        {
            $info = $userGradeModel->where('id', input('param.id'))->find();
            if(!$info)
            {
                $result['msg'] = "没有此条记录";
                return $result;
            }
            View::assign('data', $info);
        }else{
            $data = [
                'id' => '',
                'name' => '',
                'is_def'=>0
            ];
            View::assign('data', $data);
        }

        $result['status'] = 0;
        $result['msg'] = '成功';
        $result['datas'] = View::fetch('grade_edit');
        return $result;
    }


    /**
     * 用户等级删除
     * @author lin
     */
    public function gradeDel()
    {
        $result = [
            'status' => false,
            'msg' => '失败',
            'data' => ''
        ];

        $userGradeModel = new UserGrade();
        if(!input('?param.id'))
        {
            return error_code(10000);
        }

        $info = $userGradeModel->where('id', input('param.id'))->find();
        if(!$info)
        {
            $result['msg'] = "没有此用户等级";
            return $result;
        }
        $re = $userGradeModel->where('id', input('param.id'))->delete();
        if($re)
        {
            $result['status'] = true;
        }
        else
        {
            $result['msg'] = "删除失败";
        }
        return $result;
    }

    /**
     * 用户统计
     * @author lin
     * @return array
     */
    public function statistics()
    {
        $userLogModel = new UserLog();
        $list_login   = $userLogModel->statistics(7, $userLogModel::USER_LOGIN);
        $list_reg     = $userLogModel->statistics(7, $userLogModel::USER_REG);

        $data = [
            'legend' => [
                'data' => ['新增记录', '活跃记录']
            ],
            'xAxis'  => [
                [
                    'type' => 'category',
                    'data' => $list_login['day']
                ]
            ],
            'series' => [
                [
                    'name' => '新增记录',
                    'type' => 'line',
                    'data' => $list_reg['data']
                ],
                [
                    'name' => '活跃记录',
                    'type' => 'line',
                    'data' => $list_login['data']
                ]
            ]
        ];
        return $data;
    }

}
